<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class TransactionController extends AdminBaseController
{
  public function _initialize()
  {
  }
  
  /**用户交易列表*/
  public function index ()
  {
    
    $where['izy_order.bstype'] = 'buy';
    $request = input('request.');
    
    if (!empty($request['uid'])) {
      $where['izy_transaction.uid'] = array('EQ',intval($request['uid']));
    }
    
    if (!empty($request['mobile'])) {
      $query_mobile = $request['mobile'];
      $where['izy_user.mobile'] = array('LIKE', "%$query_mobile%");
    }
    
    if (!empty($request['date'])) {
      $query_time = strtotime($request['date']);
      $where['izy_transaction.ctime'] = array('EGT', $query_time);
    }
    
    $list = Db::connect('aizuoyou')
      ->name('transaction')
      ->join('user','izy_transaction.buid = izy_user.uid')
      ->join('order','izy_transaction.borderid = izy_order.id')
      ->where($where)
      ->field(array('izy_transaction.buid', 'izy_transaction.suid', 'izy_transaction.price', 'izy_transaction.coin', 'izy_transaction.money', 'izy_transaction.ctime', 'izy_order.price' => 'buyprice', 'izy_order.rcoin', 'izy_user.username' => 'buyname', 'izy_user.mobile' => 'buymobile'))
      ->order("izy_transaction.ctime DESC")
      ->paginate(10);
    
    /**处理卖家数据*/
      $transation_list = $list->items();
      
      $sallwhere = [];
    foreach ($transation_list as $key=>$value) {
      $sallwhere[] = $value['suid'];
    }
    
    $salluid['uid'] = array('IN', $sallwhere);
    
    $sall_list = Db::connect('aizuoyou')
      ->name('user')
      ->where($salluid)
      ->field(array( 'uid' => 'suid', 'username' => 'sallname', 'mobile' => 'sallmobile'))
      ->select();
    
    foreach ($transation_list as $tkey => $tvalue) {
      foreach ($sall_list as $skey => $svalue) {
        if($tvalue['suid'] == $svalue['suid']){
          $transation_list[$tkey]['sallname'] = $svalue['sallname'];
          $transation_list[$tkey]['sallmobile'] = $svalue['sallmobile'];
        }
      }
    }
    
    // 获取分页显示
    $page = $list->render();
    
    $this->assign('list', $transation_list);
    $this->assign('page', $page);
    // 渲染模板输出
    return $this->fetch();
  }
}